package cn.yuhh.servlet;

import cn.yuhh.entity.Food;
import cn.yuhh.factory.BeanFactory;
import cn.yuhh.service.IFoodService;
import cn.yuhh.service.impl.FoodService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import static jdk.nashorn.internal.objects.NativeString.search;

/**
 * Created by Brill on 2016/12/5.
 */
@WebServlet(name = "FoodServlet", urlPatterns = {"/food"})

public class FoodServlet extends HttpServlet {

    private IFoodService service = BeanFactory.getInstance("foodService", IFoodService.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("type/html;charset=utf-8");
//
//        String method = request.getParameter("method");
//
//        System.out.println(request.getParameter("method"));
//
//        if (method != null) {
//            if ("add".equals(method)) {
//                add(request, response);
//            } else if ("list".equals(method)) {
//                list(request, response);
//            } else if ("up1".equals(method)) {
//                update(request, response);
//            } else if ("delete".equals(method)) {
//                delete(request, response);
//            } else if ("search".equals(method)) {
//                search(request, response);
//            }
//        }
        list(request, response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
        Food food = new Food();
        Enumeration<String> enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String foodName = enums.nextElement();
            food.setFoodName(foodName);
            double price = Double.parseDouble(enums.nextElement());
            food.setPrice(price);
            double mPrice = Double.parseDouble(enums.nextElement());
            food.setmPrice(mPrice);
            String remark = enums.nextElement();
            food.setRemark(remark);
            String img = enums.nextElement();
            food.setImg(img);

        }
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Food> foods = service.query();
        request.setAttribute("foods", foods);


        request.getRequestDispatcher("/app/food/foodList.jsp").forward(request, response);

    }
}
